
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vng.zing.survey.data;

import static com.vng.zing.survey.data.Base.deserialize;
import static com.vng.zing.survey.data.Base.serialize;
import com.vng.zing.survey.thrift.model.SurveyStatsItem;
import com.vng.zing.survey.thrift.model.Error;
import com.vng.zing.survey.thrift.utility.Utility;
import java.io.IOException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import kyotocabinet.DB;
import org.apache.log4j.Logger;

/**
 *
 * @author kienpd
 */
public class SurveyStatsData extends Base {

    private static final Logger logger_ = Logger.getLogger(SurveyStatsData.class);
    private static final Lock createLock_ = new ReentrantLock();
    private static SurveyStatsData _instance = null;
    private final DB db;

    public SurveyStatsData() {
        db = new DB();
        if (!db.open("database/surveystats.kch"+cofigdata  , DB.OWRITER | DB.OCREATE|DB.OAUTOTRAN)) {
            System.err.println("open error: " + db.error());
        }

    }

    public static SurveyStatsData getInstance() {
        if (_instance == null) {
            createLock_.lock();
            try {
                if (_instance == null) {
                    _instance = new SurveyStatsData();
                }
            } finally {
                createLock_.unlock();
            }
        }
        return _instance;
    }

    public Error setSurveyStatsItem(int idSurvey, SurveyStatsItem surveyStatsItem) {
        try {
            byte[] key = serialize(idSurvey);
            byte[] value = serialize(surveyStatsItem);
            if (!db.set(key, value)) {
                return Error.ERR_SYSTEM;

            }
            return Error.SUCCESS;
        } catch (IOException ex) {
            logger_.error("SurveyStatsData.setSurveyStatsItem" + ex.getMessage(), ex);
        }
        return Error.ERR_SYSTEM;
    }

    public SurveyStatsItem getSurveyStatsItem(int idSurvey)  {

        try {
            byte[] key = serialize(idSurvey);
            byte[] value = db.get(key);
            if (value == null) {
                return null;
            }
            SurveyStatsItem surveyStatsItem = new SurveyStatsItem();
            surveyStatsItem = (SurveyStatsItem) deserialize(value);
            return surveyStatsItem;

        } catch (IOException ex) {
            logger_.error("SurveyStatsData.getSurveyStatsItem" + ex.getMessage(), ex);
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SurveyStatsData.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public Error removeSurveyStatsItem(int idSurvey) {
        try {
            byte[] key = serialize(idSurvey);
            if (db.seize(key) == null) {
                return Error.ERR_SYSTEM;
            }
            return Error.SUCCESS;
        } catch (IOException ex) {
            logger_.error("SurveyStatsData.removeSurveyStatsItem" + ex.getMessage(), ex);
        }
        return Error.ERR_SYSTEM;

    }

    public Error updateSurveyStatsItem(int idSurvey, SurveyStatsItem itemStats) {
            db.begin_transaction(false);
            byte[] key;
            byte[] value;
            SurveyStatsItem surveyStatsItem = new SurveyStatsItem();
            try {
                key = serialize(idSurvey);
                value = db.get(key);
                if (value == null) {
                      db.end_transaction(true);
                    return Error.IDSURVEY_NO_EXITS;
                }

                surveyStatsItem = (SurveyStatsItem) deserialize(value);

            } catch (IOException  ex) {
                logger_.error("SurveyStatsData.updateSurveyStatsItem" + ex.getMessage(), ex);
                
                return Error.ERR_SYSTEM;
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(SurveyStatsData.class.getName()).log(Level.SEVERE, null, ex);
            }

            surveyStatsItem.setView(surveyStatsItem.getView() + itemStats.getView());
            surveyStatsItem.setResponse(surveyStatsItem.getResponse() + itemStats.getResponse());
            surveyStatsItem.setStats(Utility.synchMapQuesStat(surveyStatsItem.getStats(), itemStats.getStats()));

            try {
                key = serialize(idSurvey);
                value = serialize(surveyStatsItem);
                if (!db.set(key, value)) {
                     db.end_transaction(true);
                    return Error.ERR_SYSTEM;

                }

            } catch (IOException ex) {
                logger_.error("SurveyStatsData.updateSurveyStatsItem" + ex.getMessage(), ex);
            }
           db.end_transaction(true);   
        return Error.SUCCESS;
    }
}
